import { createRouter, createWebHashHistory } from "vue-router";

const routes = [
  // 无需布局的页面
  {
    path: "/login",
    name: "Login",
    component: () => import("../views/Login.vue"),
  },
  {
    path: "/register",
    name: "Register",
    component: () => import("../views/Register.vue"),
  },

  // 需要布局的页面
  {
    path: "/",
    redirect: "/shouye",
  },
  {
    path: "/shouye",
    name: "shouye",
    component: () => import("../views/shouye.vue"),
  },
  {
    path: "/files",
    name: "files",
    component: () => import("../views/FileManager.vue"),
  },
  {
    path: "/fileupload",
    name: "fileupload",
    component: () => import("../views/fileupload.vue"),
  },

  {
    path: "/about",
    name: "About",
    component: () => import("../views/About.vue"),
  },
  {
    path: "/help",
    name: "help",
    component: () => import("../views/help.vue"),
  },
  {
    path: "/share",
    name: "share",
    component: () => import("../views/share.vue"),
  },
  {
    path: "/new",
    name: "new",
    component: () => import("../views/new.vue"),
  },
  {
    path: "/userfolders",
    name: "userfolders",
    component: () => import("../views/userfolders.vue"),
  },
  {
    path: "/details/:id",
    name: "details",
    component: () => import("../views/details.vue"),
  },
  {
    path: "/removefiles",
    name: "removefiles",
    component: () => import("../views/removefiles.vue"),
  },
  {
    path: "/renameFolder",
    name: "renameFolder",
    component: () => import("../views/renameFolder.vue"),
  },
  {
    path: "/msg",
    name: "msg",
    component: () => import("../views/msg.vue"),
  },
  {
    path: "/huishou",
    name: "huishou",
    component: () => import("../views/huishou.vue"),
  },
  {
    path: "/linkme",
    name: "linkme",
    component: () => import("../views/linkme.vue"),
  },
  {
    path: "/userinfo",
    name: "userinfo",
    component: () => import("../views/userinfo.vue"),
  },
  {
    path: "/vip",
    name: "vip",
    component: () => import("../views/vip.vue"),
  },
  {
    path: "/video-stream-test",
    name: "video-stream-test",
    component: () => import("../views/VideoStreamTest.vue"),
  },
  {
    path: "/rongliang",
    name: "rongliang",
    component: () => import("../views/rongliang.vue"),
  },
  {
    path: "/zhuxiao",
    name: "zhuxiao",
    component: () => import("../views/zhuxiao.vue"),
  },
  // 我的文件路由
  {
    path: "/myfile/videos",
    name: "myfile-videos",
    component: () => import("../views/myfile/videos.vue"),
  },
  {
    path: "/myfile/audio",
    name: "myfile-audio",
    component: () => import("../views/myfile/audio.vue"),
  },
  {
    path: "/myfile/pic",
    name: "myfile-pic",
    component: () => import("../views/myfile/pic.vue"),
  },
  {
    path: "/myfile/doc",
    name: "myfile-doc",
    component: () => import("../views/myfile/doc.vue"),
  },
  {
    path: "/myfile/others",
    name: "myfile-others",
    component: () => import("../views/myfile/others.vue"),
  },

  // 兼容旧路由
  {
    path: "/videos",
    redirect: "/myfile/videos",
  },
  {
    path: "/audio",
    redirect: "/myfile/audio",
  },
  {
    path: "/pic",
    redirect: "/myfile/pic",
  },
  {
    path: "/others",
    redirect: "/myfile/others",
  },


  {
    path: "/sharelink",
    name: "sharelink",
    component: () => import("../views/sharelink.vue"),
  },
  {
    path: "/share/:shareCode",
    name: "ShareAccess",
    component: () => import("../views/ShareAccess.vue"),
  },

  // 404页面
  {
    path: "/:pathMatch(.*)*",
    name: "NotFound",
    component: () => import("../views/404.vue"),
  },
];

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes,
});

router.beforeEach((to, from, next) => {
  if (to.path === "/login" || to.path === "/register") {
    next();
  } else {
    const token = localStorage.getItem("token");
    if (!token) {
      next("/login");
    } else {
      next();
    }
  }
});

export default router;
